package cn.damong.utils.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5工具类
 * 
 * @author CodingPanda
 * @2019年5月16日 下午8:15:44
 *
 */
public class MD5Util {

	/**
	 * 返回大写的MD5值
	 * @param s
	 * @return
	 */
	public static String getMD5(String s) {
		if(s == null){
			return null;
		}
		byte[] input = s.getBytes();
		String output = null;
		char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
				'e', 'f' };
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(input);
			byte[] tmp = md.digest();
			char[] str = new char[32];
			byte b = 0;
			for (int i = 0; i < 16; i++) {
				b = tmp[i];
				str[2 * i] = hexChar[b >>> 4 & 0xf];
				str[2 * i + 1] = hexChar[b & 0xf];
			}
			output = new String(str);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return output.toUpperCase();
	}

	public static void main(String[] args) {
		String str = MD5Util.getMD5("1sdsds");
		System.out.println(str);
	}

}
